<?php

namespace Modules\Article\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Article\Entities\Article;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $articledate = Article::paginate(8);
//        dd($articledate);
        //加载文章列表的模板
        return view('article::article.index',compact('articledate'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        //加载发布文章的模板
        return view('article::article.create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        //处理文章添加，添加到数据库
        Article::create($request->all());
        session()->flash('success','文章发布成功');
        return redirect()->route('article.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('article::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        //获取对应id的文章数据
        $article = Article::find($id);

        return view('article::article.edit',compact('article'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request,$id)
    {
        //获取对应id的文章数据
        $article = Article::find($id);
        //获取post提交数据(就是编辑后提交的数据)
        $post = $request->all();
        //对文章的对应数据进行赋值
        $article->title = $post['title'];
        $article->author = $post['author'];
        $article->content = $post['content'];
        $article->click = $post['click'];
        $article->is_commend = $post['is_commend'];
        //保存一下
        $article->save();
        //设置闪存信息,然后返回
        session()->flash('success','文章编辑成功');
        return redirect()->route('article.index');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy(\App\Http\Controllers\Controller $controller,$id)
    {
//        dd(123);
        $article = Article::find($id);
        $controller->authorize('delete',$article);
        Article::destroy($id);
        session()->flash('success','删除成功');
        return redirect()->route('article.index');
    }
}
